<?php
/**
 * Created by PhpStorm.
 * User: caoguangyang
 * Date: 2023-01-22
 * Time: 16:41
 */

namespace App\Http\Controllers\Wx;


use App\CodeResponse;
use App\Enums\Constant;
use App\Inputs\GoodsListInput;
use App\Models\Goods\GoodsAttribute;
use App\Models\Goods\GoodsProduct;
use App\Models\SearchHistory;
use App\Services\CollectServices;
use App\Services\CommentServices;
use App\Services\Goods\BrandServices;
use App\Services\Goods\CategoryServices;
use App\Services\Goods\GoodsServices;
use App\Services\SearchHistoryServices;
use Illuminate\Http\Request;

class GoodsController extends WxController
{
    protected $only = [];

    /**
     * 统计在销售的数量
     * @return \Illuminate\Http\JsonResponse
     */
    public function count() {
        $count = GoodsServices::getInstance()->countGoodsOnSale();
        return $this->success($count);
    }

    /**
     * 获取分类下的商品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function category(Request $request) {
        $id = $request->input("id", 0);
        if (empty($id)) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $cur = CategoryServices::getInstance()->getCategory($id);
        if (is_null($cur)) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        $parent = null;
        $children = null;
        if ($cur->pid == 0) {
            $parent = $cur;
            $children = CategoryServices::getInstance()->getL2ListByPid($cur->id);
            $cur = $children->first() ?? $cur;
        } else {
            $parent = CategoryServices::getInstance()->getL1ById($cur->id);
            $children = CategoryServices::getInstance()->getL2ListByPid($cur->id);
        }
        return $this->success([
           'currentCategory' => $cur,
           'parentCategory' => $parent,
           'brothreCagegory' => $children
        ]);
    }

    /**
     * 商品列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list()
    {
        $input = GoodsListInput::new();

        // 添加搜索历史记录
        if ($this->isLogin() && !empty($keyword)) {
            SearchHistoryServices::getInstance()->save($this->userId(), $keyword, Constant::SEARCH_HISTORY_FROM_WX);
        }

        // 获取商品列表
        $columns = ['id', 'name', 'brief', 'pic_url', 'is_new', 'is_hot', 'counter_price', 'retail_price'];
        // 查询商品列表
        $goodsList = GoodsServices::getInstance()->listGoods($input, $columns);

        // 获取分类信息
        $categoryList = GoodsServices::getInstance()->listL2Category($input);

        $goodsList = $this->paginate($goodsList);
        $goodsList['filterCategoryList'] = $categoryList;
        return $this->success($goodsList);
    }

    public function detail(Request $request) {
        // 接收数据
        $id = $request->input("id");
        if (empty($id)) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        // 获取商品信息
        $info = GoodsServices::getInstance()->getGoods($id);
        if (empty($info)) {
            return $this->fail(CodeResponse::PARAM_VALUE_ILLEGAL);
        }

        // 获取属性信息
        $attr = GoodsServices::getInstance()->getGoodsAttribute($id);

        // 获取商品规格信息
        $spec = GoodsServices::getInstance()->getGoodsSpecification($id);

        // 获取商品sku信息
        $product = GoodsServices::getInstance()->getGoodsProduct($id);

        // 获取商品相关问题
        $issue = GoodsServices::getInstance()->getGoodsIssue();

        // 获取商品品牌
        $brand = $info->brand_id ? BrandServices::getInstance()->getBrand($info->brand_id) : (object)[];

        // 获取商品评论
        $comment = CommentServices::getInstance()->getCommentWithUserInfo($id);

        $userHasCollect = 0;
        if ($this->isLogin()) {
            $userHasCollect = CollectServices::getInstance()->countByGoodsId($this->userId(), $id);
            // 记录用户足迹
            GoodsServices::getInstance()->saveFootPrint($this->userId(), $id);
        }

        // 团购信息
        // 系统配置
        return $this->success([
            "info" => $info,
            "userHasCollect" => $userHasCollect,
            "issue" => $issue,
            "comment" => $comment,
            "specificationList" => $spec,
            "productList" => $product,
            "attribute" => $attr,
            "brand" => $brand,
            "groupon" => [],
            "share" => false,
            "shareImage" => $info->share_url
        ]);
    }
}